package zero.tech.games.jh.util;

import java.util.List;

import zero.tech.core.ZeroContext;
import zero.tech.games.jh.data.JhConfig;
import zero.tech.games.jh.data.JhTimer;
import zero.tech.games.jh.service.api.IJhService;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.service.api.ILogicService;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.wsnet.controller.LogicController;

public class RoomUtil {

	public static void checkGameStart(Room room) {
		IJhService jhService=ZeroContext.getInstance(IJhService.class);
		JhConfig jhConfig = (JhConfig) room.getGameConfig();
		JhTimer jhTimer=(JhTimer) room.getTimer();
		List<LogicController> readyPeopers=LogicUtil.readyPeopers(room);
		if(readyPeopers.size()==room.getControllersWithoutWatcherAndTaotai().size()&&readyPeopers.size()>=2) {
			jhService.gameStart(room);
		}else {
			int room_status=room.getRoom_status();
			if(room_status==RoomStatus.WAIT_NEXT_ROUND) {
				ILogicService logicService=ZeroContext.getInstance(ILogicService.class);
				//自动准备
				int auto_ready=jhConfig.getAuto_ready();
				if(auto_ready>0) {
					//自动准备
					long now=System.currentTimeMillis();
					long lastBeginToReadyTime=jhTimer.lastBeginToReadyTime;
					List<LogicController> notReadyPeopers=LogicUtil.notReadyPeopers(room);
					if(now-lastBeginToReadyTime>=auto_ready*1000) {
						for(LogicController notReadyPeople:notReadyPeopers) {
							if(!room.isWatcher(notReadyPeople.getId())) {
								logicService.reqReady(room,notReadyPeople);
							}
						}
					}
				}
			}
		}
	}
}
